<template>
	<view class="content">
		<loading v-if="showLoading"></loading>
		<CustomNav :isBack="true" :isPack="isPick" :partnerId="id" title="详情"></CustomNav>
		<swiper style="height:460rpx;width: 100%" :indicator-dots="false" :autoplay="true" :circular="true"
			:interval="5000">
			<swiper-item v-for="(item, index) in info.file" :key="item.id">
				<image style="width: 100%;height: 100%;" :src="cloudStorage+item.url" v-if="item.fileType==1">
				</image>
				<video :src="cloudStorage+item.url" v-if="item.fileType==2" style="width: 100%;height: 100%;"></video>
			</swiper-item>
		</swiper>
		<image class="head" :src="cloudStorage+info.businessFace" mode="widthFix"></image>
		<view class="title-block">
			<view class="businessInfo">
				<view class="title-top">
					<view class="title">
						{{ info.businessName }}
					</view>
				</view>
				<view class="title-bottom">
					<view class="bus" v-if="info.businessStatus==1" style="color: #0AC500;">
						营业中
					</view>
					<view class="bus" v-if="info.businessStatus==2" style="color: #767676;">
						未营业
					</view>
					<view class="time">
						{{info.businessTime}}
					</view>
				</view>

			</view>
			<view class="shoucang" @click="handlePick">
				<image class="img"
					:src="isPicked==1? 'https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/collect.png' : 'https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/noCollect.png'">
				</image>
				<view class="collect-font">收藏</view>
			</view>
		</view>
		<view class="menu-block4 block">
			<view class="title-top">
				<view class="tips"></view>
				<view class="title">
					商家信息
				</view>
			</view>
			<view class="iphone-content" style="margin-bottom: 10rpx;">
				<view class="data" style="border-bottom: 1px solid #EEEEEE;">
					<view class="money-title">
						<image
							src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/home/jq-erq/address1.png"
							style="width: 100%;height: 100%;"></image>
					</view>
					<view class="count">
						{{info.address}}
					</view>
				</view>
				<view class="iconfont" @click="openLocation(info.latitude,info.longitude,info.businessName)">
					<image class="img"
						src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/home/jq-erq/distance1.png">
					</image>
					<view class="play" style="text-align: center;">{{info.distance}}km</view>
				</view>
			</view>
			<view class="iphone-content">
				<view class="data">
					<view class="money-title">
						<image
							src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/home/jq-erq/phone.png"
							style="width: 100%;height: 100%;"></image>
					</view>
					<view class="count">
						{{info.businessPhone}}
					</view>
				</view>
				<view class="iconfont" @click="callPhone(info.businessPhone)">
					<image class="img"
						src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/home/jq-erq/dial.png">
					</image>
					<view class="play" style="text-align: center;">拨打</view>
				</view>
			</view>
		</view>
		<view class="shop-block" v-if="activityType!=0">
			<view class="title">活动</view>
			<template v-if="preferenceList.length>0&&!activityType">
				<view class="preference" v-for="item in preferenceList" :key="item.id"
					@click="navigate('/page_other/activity/activityDetail?id='+item.id+'&type='+type)">
					<image class="preference-left" :src="cloudStorage+item.cover" mode="aspectFit"></image>
					<view class="preference-right">
						<view class="preference-rightTop">
							<view class="preference-title">
								{{item.activityName}}
							</view>
							<view class="preference-tips">
								<view class="preference-item">
									{{item.serviceGuarantee}}
								</view>
							</view>
						</view>
						<view class="preference-rightBottom" v-if="item.classification==1">
							<view class="price">
								<view class="price-top">
									<template v-if="item.classification != 1">
										<view class="current-price">
											{{item.price}}
										</view>
										<view class="original-price">
											￥{{item.originalPrice}}
											<view class="price-line">
											</view>
										</view>
									</template>
									<template v-else>
										<view class="current-price" style="font-size: 28rpx;">
											免费抢购
										</view>
									</template>
								</view>
								<view class="price-bottom">
									享{{item.preferential}}折，已售{{item.sold||0}}
								</view>
							</view>
							<view class="rob">
								<view class="rob-word FangZhengHanZhenGuangBiaoJianTi">
									抢
								</view>
								<image
									src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/robRight.png"
									mode=""></image>
							</view>
						</view>

					</view>
				</view>
			</template>
			<template v-else-if="preferenceList.length>0&&activityType==2">
				<view class="preference" v-for="item in preferenceList" :key="item.id"
					@click="navigate('/page_other/activity/activityDetail?id='+item.id+'&type='+type)">
					<image class="preference-left" :src="cloudStorage+item.cover" mode="aspectFit"></image>
					<view class="preference-right">
						<view class="preference-rightTop" style="margin-top: 30rpx;">
							<view class="preference-title">
								{{item.activityName}}
							</view>
							<view class="preference-tips" style="margin-top: 20rpx;">
								<view class="preference-item">
									{{item.serviceGuarantee}}
								</view>
							</view>
						</view>
					</view>
				</view>

			</template>
			<view v-else style="margin: 30rpx 0;display: flex;justify-content: center;align-items: center;">
				暂无活动
			</view>
		</view>
		<view class="shop-block">
			<view class="title" style="margin-bottom: 20rpx;">详情</view>
			<mp-html :content="info.businessDetails"></mp-html>
		</view>
		<view class="step fixed-button" @click="appointment" v-if="activityType==2">立即预约</view>

		<view class="menu-block3 block">
			<view class="top">
				<view class="title-top">
					<view class="title">
						评价 ({{count}})
					</view>
				</view>
				<view class="top-right" @click="navigate('/page_other/activity/comment?id='+id+'&type='+type)">
					<view class="moreText">
						查看全部
					</view>
					<image class="more"
						src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/home/jq-erq/more.png">
					</image>
				</view>
			</view>
			<template v-if="comment.length>0">
				<view class="middle" v-for="item in comment">
					<view class="head1">
						<image class="head-img" :src="cloudStorage+item.faveUrl"></image>
					</view>
					<view class="middle-right">
						<view class="middle-right-top">
							<view class="phone">
								{{item.userName}}
							</view>
							<view class="score">
								<uni-rate v-model="item.score" @change="onChange" :readonly="true" active-color="red"
									:size="14" />
							</view>
						</view>
						<view class="img-item" v-if="item.urlList.length>0">
							<view class="img-list" v-for="(item1,index) in item.urlList" :key="index">
								<image :src="cloudStorage+item1" class="img" mode="aspectFill"
									@click="previewImgs(item1,index)">
								</image>
							</view>
						</view>
						<view class="middle-right-middle">
							{{item.content}}
						</view>
						<view class="middle-right-bottom">
							{{item.createTime}}
						</view>
					</view>
				</view>
			</template>
			<view v-else style="margin: 40rpx auto;width: 150rpx;">
				暂无评价
			</view>
		</view>
		<view class="small-title" style="height: 140rpx;"></view>
	</view>
</template>

<script>
	import Tabbar from '@/components/Tabbar/Tabbar';
	import CustomNav from '@/components/CustomNav/CustomNav2';
	import {
		getStorage,
		setStorage,
		removeStorage
	} from '@/util/auth.js';
	import {
		getBusinessInfoBycode,
		reservationActivity,
		handleShareConsumerCoupon
	} from '@/api/activities.js';
	import {
		addUserBusiness,
		getBusinessActivity,
		getCommentByTypeList,
		getUserBusinessById,
		userStopBusiness,
	} from '@/api/acticity.js';

	export default {
		components: {
			Tabbar,
			CustomNav
		},
		data() {
			return {
				isPick: 2,
				info: {
					id: '',
					businessFace: '',
					businessName: '',
					businessStatus: 1,
					businessTime: '',
					address: '',
					distance: '',
					businessPhone: ''
				},
				count: 0,
				latitude: 39.74453,
				longitude: 98.51061,
				id: 0,
				comment: [],
				page: 1,
				limit: 5,
				commentType: 1,
				preferenceList: [],
				score: 4,
				swiperList: [],
				list: [],
				lat: 39.74453,
				lng: 98.51061,
				type: 0,
				activityType: 0,
				jumpActivity: 0,
				isPicked: 0
			};
		},

		async onLoad(e) {
			if (e.scene) {
				this.getcode(e.scene)
			} else {
				this.id = e.id
				if (e.type) {
					this.type = e.type
				}
				if (e.activityType) {

					this.activityType = e.activityType
				}
				if (e.jumpActivity) {
					this.jumpActivity = e.jumpActivity
				}
				if (e.latitude) {
					this.latitude = e.latitude
					this.longitude = e.longitude
				} else {
					this.latitude = getStorage('latitude')
					this.longitude = getStorage('longitude')
				}
				uni.showCustomLoading();
				Promise.all([this.getBusinessActivity(), this.getCommentByTypeList(), this.getUserBusinessById()])
					.finally(
						() => {
							uni.hideCustomLoading()
						})
			}

		},
		onShow() {},

		methods: {
			//是否收藏
			handlePick() {
				this.isPicked = this.isPicked === 1 ? 2 : 1;
				if (this.isPicked === 1) {
					this.addUserBusiness();
				} else {
					this.userStopBusiness();
				}
			},
			//用户收藏商家
			addUserBusiness() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				addUserBusiness({
					businessId: this.info.id,
				}).then((res) => {
					uni.hideLoading()
					this.getUserBusinessById()
					this.showToast('收藏成功');
				})
			},
			//用户取消此商家收藏
			userStopBusiness() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				userStopBusiness({
					businessId: this.info.id,
				}).then((res) => {
					uni.hideLoading()
					this.getUserBusinessById()
					this.showToast('取消收藏');
				})
			},
			getcode(scene) {

				const hasToken = getStorage('token');
				if (hasToken == null || hasToken == '') {
					uni.showModal({
						title: '提示',
						content: '请先登录后重新扫码！',
						cancelText: '先逛逛',
						confirmText: '去登录',
						success(res) {
							if (res.confirm) {
								uni.reLaunch({
									url: '/page_other/mine/mine'
								})
							} else {
								uni.reLaunch({
									url: '/page_other/mine/mine'
								})
							}
						}
					})
				}
				getBusinessInfoBycode({
					verificationCode: scene
				}).then(res => {
					this.jumpActivity = 5
					this.type = 2
					this.activityType = 2
					this.id = res.businessId
					setStorage('verificationCode', scene)
					uni.showCustomLoading();
					Promise.all([this.getBusinessActivity(),
						// this.getCommentByTypeList(),
						this.getUserBusinessById()
					]).finally(
						() => {
							uni.hideCustomLoading()
						})
				})
			},
			//立即预约
			appointment() {

				const cationCode = getStorage('verificationCode')
				if (cationCode != null && cationCode != '') {
					handleShareConsumerCoupon({
							verificationCode: cationCode
						}).then(res => {

						})
						.finally(() => {
							removeStorage('verificationCode')
						})
					return;
				}

				var that = this;

				uni.showModal({
					title: '提示',
					content: '您确定预约此套餐吗？',
					cancelText: '取消',
					confirmText: '确定',
					success(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '预约中',
								mask: true
							})
							reservationActivity({
								businessId: that.info.id
							}).then((res) => {
								this.isShow = false
								that.showToast('预约成功', () => {
									uni.redirectTo({
										url: '/page_other/merchant_activity/index'
									})
								}, 1000)
							})
						}
					}
				})
			},
			//根据类型获取评论列表
			getCommentByTypeList() {
				return getCommentByTypeList({
					page: this.page,
					limit: this.limit,
					commentType: this.commentType,
					relationId: this.id,
				}).then((res) => {
					this.count = res.count
					this.comment = res.list
				})
			},
			//查询此商家活动
			getBusinessActivity() {
				return getBusinessActivity({
					businessId: this.id,
					classification: this.jumpActivity
				}).then((res) => {
					this.preferenceList = res.list
				})
			},

			//商家详情
			getUserBusinessById() {
				return getUserBusinessById({
					id: this.id,
					longitude: this.longitude,
					latitude: this.latitude,
				}).then((res) => {
					this.info = res
					this.isPicked = res.isCollect
				})
			},
			previewImgs(current, index) {
				let pic = []
				pic.push(this.cloudStorage + current)
				uni.previewImage({
					current: index,
					urls: pic
				})
			},
			callPhone(e) {
				const hasToken = getStorage('token');
				const user = getStorage('user');
				const id = this.id
				if (user && hasToken) {
					uni.makePhoneCall({
						phoneNumber: e,
						success() {
							console.log('拨打成功了');
						},
						fail() {
							console.log('拨打失败了');
						}
					})
				} else {
					this.goDl()
				}
			},
			goDl() {
				const hasToken = getStorage('token');
				const user = getStorage('user');
				if (user && hasToken) {
					return true;
				} else {
					uni.showModal({
						title: '提示',
						content: '需要先登录！',
						cancelText: '先逛逛',
						confirmText: '去登录',
						success(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/page_other/login/select'
								})
							}
						}
					})

				}
			},
			onChange(e) {
				console.log(e)
			},
			navigate(url) {

				if (url) {
					uni.navigateTo({
						url
					});
				} else {
					uni.scanCode({
						onlyFromCamera: true,
						success: (res) => {
							scanJump({
								result: res.result
							}).then((res) => {
								uni.navigateTo({
									url: '/' + res.jump + '?result=' + res.number
								})
							})
						}
					});
				}
			},
		}
	};
</script>

<style scoped lang="scss">
	.content {
		width: 100%;
		overflow-y: auto;
		position: relative;

		.fixed-button {
			position: fixed;
			bottom: 20rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 450rpx;
			/* 按钮的宽度 */
			height: 100rpx;
			/* 按钮的高度 */
			display: flex;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
			box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
			color: #fff;
			border-radius: 60rpx;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;
			margin: 0 auto;
			/* 如果需要调整按钮的位置，可以调整这里的值 */

		}

		.step {
			width: 450rpx;
			height: 100rpx;
			display: flex;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
			box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
			color: #fff;
			margin: 30rpx auto;
			border-radius: 60rpx;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;

		}

		.head {
			position: absolute;
			top: 410rpx;
			right: 30rpx;
			width: 180rpx;
			height: 140rpx;
			border-radius: 20rpx;
		}

		.title-block {
			padding: 30rpx;
			display: flex;
			justify-content: start;

			.businessInfo {

				width: 420rpx;
			}

			.shoucang {

				width: 80rpx;
				height: 80rpx;
				display: flex;
				flex-direction: column;

				.img {
					margin: auto 0;
					width: 50rpx;
					height: 50rpx;
				}

				.collect-font {
					font-size: 28rpx;
					margin-top: 5rpx;
				}
			}

			.title-top {
				display: flex;
				align-items: flex-start;
				margin-bottom: 30 rpx;

				.collect-item {
					display: flex;
					width: 200 rpx;
					margin-right: 260 rpx;
					height: 40 rpx;

					.collect-text {
						margin-left: 10 rpx;
						font-size: 28 rpx;
						line-height: 40 rpx;
						font-weight: normal;
					}
				}

				.title {
					font-size: 36 rpx;
					color: #1A1A1A;
					// width: 50%;
					// margin-right: 20rpx;
				}

				font-weight: bold;

				image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.title-bottom {
				display: flex;
				align-items: center;
				margin-bottom: 0;

				.bus {
					font-size: 28rpx;
					margin-right: 8rpx;
				}

				.time {
					font-size: 28rpx;
				}
			}
		}

		.menu-block4 {
			width: 92%;
			margin: 0 auto 20rpx;
			background: #fff;
			border-radius: 20px;
			padding: 30rpx;

			.classify-tab-block {
				width: 100%;
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
				position: relative;
				margin-top: 20rpx;

				.tab-content {
					display: flex;
					flex-direction: row;
					overflow-x: scroll;

					.district {

						.img {
							width: 280rpx;
							height: 180rpx;
							border-radius: 10rpx;
							margin: 0 10rpx;
							// width: 100%;
							// height: 100%;
						}
					}

				}

			}

			.text {
				line-height: 60rpx;
			}

			.item {
				margin: 13rpx 15rpx 0 0;
				width: 100%;
				display: flex;
				flex-wrap: wrap;

				.skill-item {
					width: calc((100% -20rpx)/5);
					height: 65rpx;
					border-radius: 60rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					background-color: #FFE6E7;
					color: red;
					margin: 0 25rpx 25rpx 0;
				}
			}

			.skill {
				display: flex;
				flex: wrap;
			}

			.title {
				font-weight: 900;
				font-size: 32rpx;
				line-height: 60rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 1;
				margin-bottom: 25rpx;
			}

			.data {
				width: calc((100% - 150rpx));
				display: flex;
				color: #767676;
				font-size: 28rpx;
				color: #1A1A1A;
				margin-top: 35rpx;
			}

			.count {
				width: calc((100% - 80rpx)/1);
				margin: 0 0 0 10rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				margin-bottom: 25rpx;
			}

			.project-content {
				line-height: 60rpx;
			}

			.iphone-content {
				display: flex;
				justify-content: space-between;

				.money-title {
					width: 44rpx;
					height: 42rpx;
				}

				.iconfont {
					width: 120rpx;
					display: flex;
					flex-direction: column;
					align-items: center;

					.play {
						color: #767676;
						font-size: 28rpx;
					}

					.img {
						margin: 0 auto 10rpx;
						width: 50rpx;
						height: 50rpx;
					}
				}
			}
		}

		.shop-block {
			width: 92%;
			margin: 0 auto 40rpx;
			padding: 30rpx;
			background-color: #fff;
			border-radius: 20rpx;

			.title {
				font-weight: bold;
				font-size: 32rpx;
				color: #1A1A1A;
			}

			.preference {
				display: flex;
				margin-top: 26rpx;

				.preference-left {
					width: 220rpx;
					height: 220rpx;
					margin-right: 20rpx;
					border-radius: 20rpx;
				}

				.preference-right {
					width: calc(100% - 240rpx);

					.preference-rightTop {
						.preference-title {
							color: #1A1A1A;
							font-weight: bold;
							font-size: 32rpx;
						}

						.preference-tips {
							display: flex;
							margin-top: 12rpx;

							.preference-item {
								padding: 0 12rpx;
								border: 1px solid #FF2F34;
								color: #FF2F34;
								border-radius: 6rpx;
								margin-right: 10rpx;
								font-size: 24rpx;
							}

							.preference-item:last-child {
								margin-right: 0;
							}
						}
					}

					.preference-rightBottom {
						display: flex;
						margin-top: 20rpx;

						.price {
							width: 75%;
							// padding-left: 40rpx ;
							padding: 10rpx 0 10rpx 40rpx;
							background-color: #FEF3EF;
							border-radius: 50px 0 0 50px;

							.price-top {
								display: flex;
								align-items: center;
								position: relative;

								.current-price {
									font-size: 36rpx;
									color: #FF2F34;
									margin-right: 8rpx;
								}

								.original-price {
									font-size: 24rpx;
									color: #FF2F34;

									.price-line {
										position: absolute;
										top: 22rpx;
										background-color: #FF2F34;
										width: 55rpx;
										height: 3rpx;
									}
								}
							}

							.price-bottom {
								font-size: 20rpx;
								color: #FF2F34;
							}
						}

						.rob {
							display: flex;
							align-items: center;
							justify-content: center;
							width: 25%;
							background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
							border-radius: 0px 50px 50px 0px;

							.rob-word {
								font-size: 36rpx;
								color: #FFFFFF;
								margin-right: 7rpx;
							}

							image {
								width: 20rpx;
								height: 20rpx;
							}
						}
					}
				}
			}
		}

		.menu-block3 {
			width: 92%;
			margin: 30rpx auto;
			background: #fff;
			border-radius: 12px;
			padding: 30rpx;

			// margin-bottom: 50rpx;
			.top {
				width: 100%;
				height: 44px;
				display: flex;
				justify-content: space-between;

				// align-items: center;
				.title {
					font-weight: 900;
					font-size: 32rpx;
					line-height: 60rpx;
					margin-bottom: 25rpx;
				}

				.top-right {
					display: flex;
					margin-top: 10rpx;

					.moreText {
						color: #999;
						font-size: 28rpx;
					}

					.more {
						width: 30rpx;
						height: 30rpx;
						color: #999;
						margin-top: 9rpx;
					}
				}
			}

			.middle {
				display: flex;
				width: 100%;

				.head1 {
					width: 20%;
					border-radius: 80rpx;

					.head-img {
						width: 100rpx;
						height: 100rpx;
						background-color: #999;
						border-radius: 80rpx;
					}

				}

				.middle-right {
					width: 80%;

					.middle-right-top {
						margin-top: 12rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-around;

						.phone {
							color: #000;
							font-size: 28rpx;
						}

					}

					.img-item {
						display: flex;
						flex-wrap: wrap;
						margin-top: 20rpx;

						.img-list {
							width: calc((100% - 20rpx)/2);
							margin-right: 20rpx;
							margin-bottom: 20rpx;

							.img {
								width: 100%;
								height: 160rpx;
								border-radius: 20rpx;
							}
						}

						.img-list:nth-child(2n) {
							margin-right: 0;
						}
					}

					.middle-right-middle {
						font-size: 28rpx;
						margin-top: 20rpx;
						color: #1A1A1A;
						line-height: 48rpx;
					}

					.middle-right-bottom {
						font-size: 28rpx;
						margin-top: 20rpx;
						color: #767676;
					}
				}

			}
		}

		.info-block {
			// position: relative;
			// background-color: #f7f6f9;
			// margin-top: -60rpx;
			// border-radius: 50rpx 50rpx 0 0;
			// padding: 30rpx;
		}
	}
</style>